package com.example04.service.impl;

import com.example04.service.StudentService;
import com.example04.entity.Student;
import com.example04.resource.DatabaseUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class StudentServiceImpl implements StudentService {
    public  List<Student> addStudent(Student student) {
        DatabaseUtils.listStudent().add(student);
        return DatabaseUtils.listStudent();
    }
    public List<Student> listStudentsByYear(int year) {
        return DatabaseUtils.listStudent()
                .stream()
                .filter(s -> s.getYear() == year)
                .collect(Collectors.toList());
    }
    public List<String> listStudentsNames(int year, Student.Sex sex) {
        return DatabaseUtils.listStudent()
                .stream()
                .filter(s -> s.getYear() == year)
                .filter(s -> s.getSex() == sex)
                .map(Student::getName)
                .collect(Collectors.toList());
    }
    public List<Student> listStudentsByYearSortedById(int year) {
        return DatabaseUtils.listStudent()
                .stream()
                .filter(s -> s.getYear() == year)
                .sorted(Comparator.comparing(Student::getId).reversed())
                .collect(Collectors.toList());
    }
    public Map<Student.Sex, List<Student>> listStudentsMapBySex() {
        return DatabaseUtils.listStudent()
                .stream()
                .collect(Collectors.groupingBy(Student::getSex));

    }
    public Map<Integer, Student> listStudentsByYearMapById(int year) {
        return DatabaseUtils.listStudent()
                .stream()
                .filter(s -> s.getYear() == year)
                .collect(Collectors.toMap(Student::getId, s -> s));
    }
    public boolean removeStudent(List<Student> students, int id) {
        students.remove(id);
        List<Student> students1 = students
                .stream()
                .filter(s -> s.getId() == id)
                .toList();
        return students1.isEmpty();
    }
}